package com.example.demo.controller;

import com.example.demo.annotation.Api;
import com.example.demo.domain.PageResult;
import com.example.demo.domain.exam.Exam;
import com.example.demo.domain.exam.ExamPaper;
import com.example.demo.domain.exam.ExamUserScore;
import com.example.demo.domain.exam.QueryModel;
import com.example.demo.domain.exam.condition.ExamUserScoreCondition;
import com.example.demo.dto.ExamUserScoreDTO;
import com.example.demo.dto.ResponseDto;
import com.example.demo.service.exam.ExamSituationService;
import com.example.demo.service.exam.ExamUserScoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 考试详情接口
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/exam/situation")
public class ExamSituationController {

    private final ExamSituationService examSituationService;

    private final ExamUserScoreService examUserScoreService;

    /**
     * 获取用户答题信息
     *
     * @param examId
     * @param userId
     * @return
     */
    @Api
    @RequestMapping(value = "user/ans/get", method = RequestMethod.GET)
    public ExamUserScoreDTO getUserAns(@RequestParam Long examId, @RequestParam Long userId) {
        return examSituationService.getExamUserScore(examId, userId);
    }

    /**
     * 获取试卷详细信息，包含答案；但编程题无答案
     *
     * @param paperId
     * @return
     */
    @Api
    @RequestMapping(value = "paper/get", method = RequestMethod.GET)
    public ExamPaper getExamPaper(@RequestParam Long paperId, String paperType) {
        return examSituationService.getExamPaper(paperId, paperType);
    }

    /**
     * 导出提交记录
     *
     * @param examId   考试标识
     * @param response 响应请求
     */
    @Api
    @RequestMapping(value = "submit/export", method = RequestMethod.POST)
    public void exportSubmitLog(@RequestParam Long examId, HttpServletResponse response) {
        examSituationService.exportSubmitLog(examId, response);
    }

    /**
     * 导出某场考试成绩接口
     * @param examId 考试id
     * @param response 文件下载
     */
    @Api
    @RequestMapping(value = "score/export", method = RequestMethod.POST)
    public void exportScore(@RequestParam Long examId, HttpServletResponse response) {
        examSituationService.exportScore(examId, response);
    }

    /**
     * 获取某场考试
     * @param model 条件实体
     * @return 返回考试
     */
    @Api
    @RequestMapping(value = "score/page", method = RequestMethod.POST)
    public PageResult<ExamUserScore> page(@RequestBody QueryModel<ExamUserScoreCondition> model) {
        return examUserScoreService.page(model);
    }

    /**
     * 获取最近几场考试
     */
    @Api
    @RequestMapping(value = "recent/exam/get", method = RequestMethod.GET)
    public List<Exam> getRecentExam(@RequestParam Long num) {
        return examSituationService.getRecentExam(num);
    }

    /**
     * 获取用户数量、考试班级数量、试卷数量、题库题目数量
     */
    @Api
    @RequestMapping(value = "statistics", method = RequestMethod.GET)
    public Map<String, Object> getStatistics() {
        return examSituationService.getStatistics();
    }

}
